/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package views;



import chatsystem.ChatSystemControler;
import chatsystem.ChatSystemModel;
import chatsystem.User;
import chatsystem.patternObserver.Observer;
import chatsystem.patternState.OfflineState;
import chatsystem.patternState.OnlineState;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author remires
 */
public class InvitConversationView extends JFrame implements Observer, WindowListener {

    
    private JPanel contentPane;
    private JPanel panelInvit;

    // Composants du panel Online
    private JButton btnInvit;
    private JList listConnectedUsers;

    // Instance de l'objet contrôleur
    private ChatSystemControler csc;

    public InvitConversationView(final ChatSystemControler csc) {

        this.csc = csc;
        this.setSize(200,500);
        this.setTitle("Inviter un ami");
        initComponents();
        //setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.getRootPane().setDefaultButton(btnInvit);

        this.csc = csc;
        this.setContentPane(contentPane);
        this.setVisible(true);
    }

    private void initComponents() {

        contentPane = new JPanel(new BorderLayout());
        //setBounds(100, 100, 450, 300);
        construirePanelInvit();

        contentPane.add(panelInvit, BorderLayout.NORTH);
    }



    private void construirePanelInvit() {

        panelInvit = new JPanel();
        contentPane.add(panelInvit, BorderLayout.NORTH);
        panelInvit.setLayout(new GridLayout(2, 2, 1, 1));

       

        btnInvit = new JButton("Inviter à la conversation");
        btnInvit.setEnabled(false);
        btnInvit.addActionListener(new ButtonListener());
        panelInvit.add(btnInvit);

        /*JLabel lblNewLabel_1 = new JLabel("Ils sont là");
        contentPane.add(lblNewLabel_1, BorderLayout.NORTH);*/

        listConnectedUsers = new JList();
        listConnectedUsers.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        listConnectedUsers.setSelectedIndex(0);

        listConnectedUsers.addListSelectionListener(new ListListener());

        JScrollPane listScroller = new JScrollPane(listConnectedUsers);
        listScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        contentPane.add(listScroller, BorderLayout.CENTER);
        //contentPane.add(listConnectedUsers, BorderLayout.CENTER);

        // on recupere la liste d'utilisateurs connectés dans le modele.
        listConnectedUsers.setModel(csc.getModel());

        //this.pack();
        this.setVisible(true);
    }

    public void windowOpened(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowClosing(WindowEvent e) {
        try {
            // TODO Demander confirmation de se déconnecter
            csc.disconnect();
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    public void windowClosed(WindowEvent e) {

    }

    public void windowIconified(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeiconified(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowActivated(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void windowDeactivated(WindowEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /* Listener de sélection de la liste */
    class ListListener implements ListSelectionListener{

        public void valueChanged(ListSelectionEvent e) {

            if (e.getValueIsAdjusting() == false) {
                if (listConnectedUsers.getSelectedIndex() == -1) {
                    //No selection, disable new talk button.
                    btnInvit.setEnabled(false);
                } else {
                    //Selection, enable the fire button.
                    btnInvit.setEnabled(true);
                }
            }
        }
    }

    /* Listener du bouton Se connecter */
    class ButtonListener implements ActionListener{

        public void actionPerformed(ActionEvent e) {

            // CLIC sur le bouton Invit
            if (e.getSource() == btnInvit) {
                try {
                    int[] selectedIndex = listConnectedUsers.getSelectedIndices();

                    for (User u : getUsersByIndex(selectedIndex)) {
                        System.out.println(""+u.getName()+" a été invité a la conversation");
                    }
                    csc.invitUser(getUsersByIndex(selectedIndex),csc.getConversation_view().getCurrent_conversation().getTid());
                } catch (Exception ex) {
                    System.out.println(ex);
                }
            }
        }
    }

    private ArrayList<User> getUsersByIndex(int[] selectedIndex) {
        ArrayList<User> selectedUsers = new ArrayList<User>();

        for (int i : selectedIndex) {
            String name = (String) listConnectedUsers.getModel().getElementAt(i);
            selectedUsers.add(csc.getUserByName(name));
        }

        return selectedUsers;
    }

    /*
     * Implémentation du Pattern Observer
     */
    
    public void update(ChatSystemModel cs) {

        // TODO Mise à jour de la liste de contacts
      
    
    }
}
